package prison.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw.visitMap.ToastUtil;
import com.shwread.android.qysw10000000.R;
import com.shwread.android.ui.widget.refresh.PullToRefreshScrollView;
import com.shwread.android.ui.widget.refresh.PullToRefreshUtil;
import com.shwread.android.ui.widget.refresh.base.PullToRefreshBase;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import logic.constants.Const;
import prison.adapter.LetterPushAdapter;
import prison.bean.LetterPushBean;
import prison.view.DividerItemDecoration;
import prison.view.FullyLinearLayoutManager;
/**
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::  FUCK ME
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 *
 * Created by lex on 2017/3/8.
 * <p>
 * 监狱项目---->家属客户端----->书信推送页面
 */

public class LetterPushActivity extends BaseSimpleActivity {


    @InjectView(R.id.recyclerView)
    RecyclerView recyclerView;
    @InjectView(R.id.refreshView)
    PullToRefreshScrollView refreshView;
    @InjectView(R.id.common_header_back)
    Button btnBack;
    @InjectView(R.id.common_header_name)
    TextView tvTitle;
    @InjectView(R.id.tVLetterEmpty)
    TextView tVLetterEmpty;

    private List<LetterPushBean.MessagesListBean> list = null;
    private int pageNum=1;
    private final int PAGESIZE = 10;
    private LetterPushAdapter adapter = null;
    private int totalPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_letter_push);
        ButterKnife.inject(this);
        initView();
    }

    void initView() {
        list = new ArrayList<>();
        btnBack.setVisibility(View.VISIBLE);
        tvTitle.setText("书信推送");
        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(this);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        adapter = new LetterPushAdapter(this,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new LetterPushAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {

            }
        });
        PullToRefreshUtil.initLabel(this, refreshView);
        refreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (refreshView.isHeaderShown()) { //下拉刷新
                    refresh();
                } else if (refreshView.isFooterShown()) { //上拉加载
                    loadMore();
                }
            }
        });
        NewActionHelp.getMessageByKinId(this, Const.UserStaffId,pageNum+"",PAGESIZE+"",new GetMessageByKinId());
    }

    @OnClick(R.id.common_header_back)
    void finish(View v) {
        finish();
    }

    /**
     * 刷新数据
     */
    void refresh(){
        pageNum=1;
        NewActionHelp.getMessageByKinId(this, Const.UserStaffId,pageNum+"",PAGESIZE+"",new GetMessageByKinId());
    }

    /**
     * 加载更多数据
     */
    void loadMore(){
        if (pageNum<totalPage){
            pageNum++;
            NewActionHelp.getMessageByKinId(this, Const.UserStaffId,pageNum+"",PAGESIZE+"",new GetMessageByKinId());
        }else{
            PullToRefreshUtil.setLoadLabel(refreshView, "没有更多数据了");
            refreshView.onRefreshComplete();
        }
    }

    private class GetMessageByKinId
            extends
            ObjectHttpResponseHandler<LetterPushBean> {

        @Override
        public Type getType() {
            return new TypeToken<LetterPushBean>() {
            }.getType();
        }

        @Override
        public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
            refreshView.onRefreshComplete();
            Log.e("书信推送接口返回数据","失败"+"---------->"+arg2);
            ToastUtil.show(LetterPushActivity.this,"获取数据失败");
        }

        @Override
        public void onSuccess(int arg0, Header[] arg1, LetterPushBean obj, String arg3) {
            Log.e("书信推送接口返回数据","成功"+"---------->"+arg3+"----------------->"+obj.toString());
            refreshView.onRefreshComplete();
            totalPage = obj.getPageMsg().getTotalPage();
            if (pageNum==1){
//                obj.getMessagesList().clear();
                if (obj.getMessagesList().size()==0){
                    tVLetterEmpty.setVisibility(View.VISIBLE);
                }else{
                    tVLetterEmpty.setVisibility(View.GONE);
                    list.clear();
                    list.addAll(obj.getMessagesList());
                }
            }else{
                list.addAll(obj.getMessagesList());
            }
            adapter.notifyDataSetChanged();
        }
    }
}
